# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: crawl.py
"""
import requests
import re
# 501018
from loguru import logger
def get_data(id):
    try:
        res1 = requests.get('http://fund.eastmoney.com/js/fundcode_search.js?v=20220325212048').text
        ff = re.findall('var r = (.*?);',res1)
        params_data = eval(ff[0])
        title = ''
        for i in params_data:
            # 判断用户输入的ID 是否在基金ID里面
            if id == i[0]:
                # 获取标题
                title  = i[2]
            # 这条地址 能根据ID 展示不同的数据
        url = f'https://danjuanapp.com/djapi/fund/nav-growth/{id}?day=30'
        res = requests.get(url,headers={
            'user-agent':'123123123'
        })
        if res.status_code == 200:
            items = res.json()
            times = []
            zf = []
            for item in items.get('data')['fund_nav_growth']:
                date = item.get('date')
                gr_per = item.get('gr_per')
                # 2023
                params_date = date.replace( date.split('-')[0] + '-','')
                times.append(params_date)
                zf.append(gr_per)
            logger.info({'time':times,'price':zf,'title':title})
            return {'time':times,'price':zf,'title':title}

    except Exception as e:
        print(e)
        return ''

get_data(501018)
